package com.zhehekeji.opsys.api.plugin.wechat.util;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

/**
 * @ClassName DESedeUtils
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2018-1-22   下午4:22:37
 */
public abstract class DESedeUtils {
	
	public static void main(String[] args) throws Exception {
		long timestamp = System.currentTimeMillis();
		//long timestamp = 1517552750679l;
		//通用
		//String plaintext = "activityCode=8FA0261C8768401C9A2FA3E406E7D15F&userID=075544330785&timestamp=" + timestamp + "&key=5e9404688667d5da671ad646b67f97f76e8ac4d3ef62b554";
		//定制，只发红包，不做活动规则
		String plaintext = "activityCode=8FA0261C8768401C9A2FA3E406E7D15F&userID=075544330786&amount=100&limitTimes=1&limitCycle=0&timestamp=" + timestamp + "&key=5e9404688667d5da671ad646b67f97f76e8ac4d3ef62b554";
		//String plaintext = "activityCode=8FA0261C8768401C9A2FA3E406E7D15F&userID=redpack05&limitWinNum=5&timestamp=" + timestamp + "&key=5e9404688667d5da671ad646b67f97f76e8ac4d3ef62b554";
		//String plaintext = "userID=075544330782&amount=100&limitTimes=1&timestamp=" + timestamp + "&key=5e9404688667d5da671ad646b67f97f76e8ac4d3ef62b554";
		String hexKey = "5e9404688667d5da671ad646b67f97f76e8ac4d3ef62b554";
		String ciphertextHex = encrypt(plaintext, hexKey);
		System.out.println(ciphertextHex);
		System.out.println(decrypt(ciphertextHex, hexKey));
		System.out.println(generateHexKey());
	}
	
	/**
	 * 生成加密密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String generateHexKey() throws NoSuchAlgorithmException{
		//生成密钥Key
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
		//必须是112或者168
		keyGenerator.init(168);
		SecretKey secretKey = keyGenerator.generateKey();
		byte[] bytesKey = secretKey.getEncoded();
		return ConvertUtils.bytesToHex(bytesKey);
	}
	
	/**
	 * 加密
	 * @param plaintext 明文
	 * @param hexKey 加密密钥
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String plaintext, String hexKey) throws Exception{
		//key转换
		DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(ConvertUtils.hextoBytes(hexKey));
		SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
		Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
		//加密
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
		byte[] encodeResult = cipher.doFinal(plaintext.getBytes());
		String ciphertextHex = ConvertUtils.bytesToHex(encodeResult); 
		return ciphertextHex;
	}
	
	/**
	 * 解密
	 * @param hexCiphertext 密文
	 * @param hexKey 密钥
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String hexCiphertext, String hexKey)throws Exception{
		//key转换
		DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(ConvertUtils.hextoBytes(hexKey));
		SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
		Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
				
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
		byte[] decodeResult = cipher.doFinal(ConvertUtils.hextoBytes(hexCiphertext));
		return new String (decodeResult);
	}
}

